/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolsKit.DAO;

import toolsKit.csv.CsvFileWriterImpl;
import toolsKit.csv.CsvFileWriter;
import toolsKit.csv.CsvFile06;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javaapplication5.model.Contact;

/**
 *
 * @author mathieubakrim
 */


// Based on example CsvChienDao12
public class CsvContactDao extends AbstractContactDao implements ContactDao{
    private CsvFileWriter fileWriter;
    
    private CsvContactDao(){
        super();
    }
    
    public CsvContactDao(File file){
        this();
        
        if (file == null){
            throw new IllegalArgumentException("File file cannot be null");
        }
        
        this.csvFile = new CsvFile06(file, ';', true);
    }
    
    public List<Contact> findAllContacts(){
        return findAllContactsByMap();
    }


    @Override
    public void writeContacts(List<Contact> contacts, File file) {
        if(contacts == null){
            throw new IllegalArgumentException("List cannot be null");
        }
        
        if (file == null){
            throw new IllegalArgumentException("File cannot be null");
        }
        
        fileWriter = new CsvFileWriterImpl(file);
        
        List<Map<String, String>> mappedData = new ArrayList<Map<String, String>>();
        for (Contact contact : contacts){
            Map<String, String> oneData = contactToMap(contact);
            mappedData.add(oneData);
        }
        fileWriter.write(mappedData);
    }
    
    private Map<String, String> contactToMap(Contact contact){
        Map<String, String> oneData = new HashMap<String, String>();
        
        oneData.put("lastName", contact.getLastName().toString());
        oneData.put("firstName", contact.getFirstName().toString());
        oneData.put("phoneNumber", contact.getPhoneNumber().toString());
        oneData.put("addressPro", contact.getAddressPro().toString());
        oneData.put("addressPer", contact.getAddressPro().toString());
        oneData.put("homePage", contact.getHomePage().toString());
        oneData.put("emailPro", contact.getEmailPro().toString());
        oneData.put("emailPer", contact.getEmailPer().toString());
        
        return oneData;
    }

    @Override
    public List<Contact> findContactByLastName(String lastName) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }




   


    
}
